<template>
<onekit-page>
<onekit-view onekit-class="container">
	<div is="onekit-template-head" :DATA="`${JSON.stringify({title:'mDNS'})}`"></div>
	<onekit-view onekit-class="page-body">
		<onekit-view onekit-class="page-section">
			<onekit-button type="primary" @Tap="startDiscovery">开始搜索 mDNS</onekit-button>
			<onekit-button type="primary" @Tap="stopDiscovery">停止搜索 mDNS</onekit-button>
			<onekit-view>
				<onekit-view v-for="item in serviceList" :key="item.index" onekit-class="row">
					<onekit-text>设备服务名：{{item.serviceName}}</onekit-text>
					<onekit-text>ip地址：{{item.ip}}:{{item.port}}</onekit-text>
					<onekit-text>当前服务类型：{{item.serviceType}}</onekit-text>
				</onekit-view>
			</onekit-view>
		</onekit-view>
	</onekit-view>
</onekit-view>
</onekit-page></template>
<script>
/* eslint-disable */
import Vue from 'vue';
import onekit_template_0 from '../../../common/head.head.vue';Vue.component('onekit-template-head',onekit_template_0);
import onekit_template_1 from '../../../common/foot.foot.vue';Vue.component('onekit-template-foot',onekit_template_1);
const ONEKIT_JSON= {
  "navigationBarTitleText": "mDNS",
  "usingComponents": {
    
  }
};
const ONEKIT_SJS = {
};
import wx from '../../../../weixin2vue/wx.js';
import Macro from '../../../../weixin2vue/Macro.js';
const {getApp,getCurrentPages} = Macro;
import Page from '../../../../weixin2vue/OnekitPage.js';
import Component from '../../../../weixin2vue/OnekitComponent.js';
let serviceList = [
];
let resolveFailList = [
];
export default Page(ONEKIT_JSON,["serviceList"],ONEKIT_SJS,{
    "onShareAppMessage"(){
      return {
        "title":'mdns',
        "path":'packageAPI/pages/mdns/mdns'
};
    },
    "daga":{
        "serviceList":[
        ],
        "resolveFailList":[
        ]
},
    "onShow"(){
      this.onLocalService();
    },
    "startDiscovery"(){
      wx.startLocalServiceDiscovery({
        "serviceType":'_http._tcp.',
        "success"(res){
          console.log(res);
          wx.showToast({
            "title":'开启成功',
            "icon":'none',
            "duration":2000
});
        },
        "fail":(err)=>{
          wx.showToast({
              "title":'开启失败',
              "icon":'none',
              "duration":2000
});
          console.log(err);
        },
        "complete":()=>{console.log('startDiscovery: complete')}
});
    },
    "stopDiscovery"(){
      const that = this;
      wx.stopLocalServiceDiscovery({
        "success":(res)=>{
          wx.showToast({
              "title":'关闭成功',
              "icon":'none',
              "duration":2000
});
          serviceList = [
          ];
          resolveFailList = [
          ];
          that.setData({
              "serviceList":[
              ],
              "resolveFailList":[
              ]
});
        },
        "fail":()=>{
          console.log('stopDiscovery: fail');
          wx.showToast({
              "title":'关闭失败',
              "icon":'none',
              "duration":2000
});
        },
        "complete":()=>{console.log('stopDIscovery: complete')}
});
    },
    "onLocalService"(){
      let that = this;
      wx.onLocalServiceFound(function(obj){
      console.log(obj);
      serviceList.push(obj);
      that.setData({
        serviceList
});
    });
      wx.onLocalServiceResolveFail(function(obj){
      console.log(obj);
      resolveFailList.push(obj);
      that.setData({
        resolveFailList
});
    });
      wx.onLocalServiceLost(function(obj){
      console.log(obj);
    });
      wx.onLocalServiceDiscoveryStop(function(obj){
      console.log('监听到搜索停止事件');
    });
    },
    "offLocalService"(){
      console.log('是否执行此部分数据');
      wx.offLocalServiceFound(function(){
      console.log('取消监听服务发现事件 成功');
    });
      wx.offLocalServiceResolveFail(function(){
      console.log('取消监听 mDNS 服务解析失败的事件 成功');
    });
      wx.offLocalServiceLost(function(){
      console.log('取消监听服务离开事件 成功');
    });
      wx.offLocalServiceDiscoveryStop(function(){
      console.log('取消监听 mDNS 服务停止搜索的事件 成功');
    });
    }
});

</script>
<style 
 lang="css">
</style>